<?php


namespace App\Http\Services\PhotoAI;


use App\Exceptions\Api\ImgAIException;
use Illuminate\Support\Collection;

class AipImageService
{
    protected $client;

    public function __construct()
    {
        $app_id = env('AI_APP_ID');
        $api_key = env('AI_APP_KEY');
        $secret_key = env('AI_SECRET_KEY');
        $this->client = new AipImageClassify($app_id,$api_key,$secret_key);
    }

    public function make_options(Collection $request,$keys = []){
        $options = [];
        foreach ($keys as $key){
            $options[$key] = $request->get($key);
        }
        return $options;
    }

    public function dish($image,$options=[]){
        $res =  $this->client->dishDetect($image,$options);
        if (array_key_exists( 'error_code',$res)){
            throw new ImgAIException($res['error_msg'],$res['code']);
        }
        return $res;
    }

    public function plant($image,$options=[]){
        $res =  $this->client->plantDetect($image,$options);
        if (array_key_exists( 'error_code',$res)){
            throw new ImgAIException($res['error_msg'],$res['error_code']);
        }
        return $res;
    }
}
